import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './registerServiceWorker'
import './plugins/element.js'
import echarts from 'echarts'
import axios from 'axios'
import './theme/global.less';
import NProgress from 'nprogress' // 引入nprogress插件
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import { hasPermission, hasRole,hasRolePermission} from './plugins/permissionDirct'
const Plugins = [hasPermission, hasRole, hasRolePermission]
Plugins.map((plugin) => {
  Vue.use(plugin)
})

Vue.config.productionTip = false
Vue.prototype.$echarts = echarts

// 创建 axios 实例并配置
const service = axios.create({
  baseURL: 'http://localhost:9001',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json;charset=utf-8'
  }
})

/**
 * 请求拦截器
 */
service.interceptors.request.use(config => {
  NProgress.start()
  config.headers.Authorization = window.sessionStorage.getItem('JWT_TOKEN')
  config.headers['Content-Type'] = 'application/json;charset=utf-8'
  console.log("config", config)
  return config
}, error => {
  return Promise.reject(error)
})

service.interceptors.response.use(
  function (response) {
    NProgress.done()
    if (response.data.code == 40300) {
      window.sessionStorage.clear()
      var vm = new Vue()
      vm.$message.error("您的登入状态已失效,请您重新登入~")
      return router.push("/login")
    } else {
      return Promise.resolve(response.data)
    }
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 使用配置好的 service 实例替代原始的 axios
Vue.prototype.$axios = service

Vue.use(ElementUI)

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
